package com.foreveross.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.HrOptimizedDao;
import com.foreveross.dao.OptimizedHouseDAO;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.HrOptimized;
import com.foreveross.vo.OptimizedHouse;

@Service("optimizedHouseService")
@Transactional
public class OptimizedHouseService extends BaseService {

	@Resource
	HrOptimizedDao optimizedInfoDAO;
	
	@Resource
	OptimizedHouseDAO optimizedHouseDAO;
	
	/**
	 * 管理员发布润优选
	 * @param message
	 * @param houseIds
	 * @throws Exception
	 */
	public void addHrOptimized(HrOptimized hrOptimized,String houseIds) throws Exception{
		if(null != hrOptimized.getId() && !"".equals(hrOptimized.getId())){
			boundOptimized(hrOptimized, houseIds);
		}else{
			hrOptimized.setIsAll(Constants.IS_ALL);
			hrOptimized.setCreateTime(new Date());
			optimizedInfoDAO.save(hrOptimized);
			if(null != houseIds && !"".equals(houseIds)){
				hrOptimized.setIsAll(Constants.NOT_ALL);
				optimizedInfoDAO.updateObject(hrOptimized);
				for(String houseId : houseIds.split(",")){
					HrHouses house = new HrHouses(houseId);
					
					OptimizedHouse messageHouse = new OptimizedHouse(house, hrOptimized);
					optimizedHouseDAO.save(messageHouse);
				}
			}
		}
	}
	
	/**
	 * 润优选绑定楼盘
	 * @param favorableId
	 * @param houseIds
	 */
	private void boundOptimized(HrOptimized optimized,String houseIds) throws Exception{
		String optimizedId = optimized.getId();
		deleteOptimizedHouseByOptimizedId(optimizedId);
		if(null != houseIds && !"".equals(houseIds)){
			optimized.setIsAll(Constants.NOT_ALL);
			optimizedInfoDAO.updateObject(optimized);
			for(String houseId : houseIds.split(",")){
				HrHouses house = new HrHouses(houseId);
				if(getOptimizedHouseListCount(optimizedId, houseId)==0){
				OptimizedHouse optimizedHouse = new OptimizedHouse(house, optimized);
				optimizedHouseDAO.save(optimizedHouse);
				}
			}
		}else{
			optimized.setIsAll(Constants.IS_ALL);
			optimizedInfoDAO.updateObject(optimized);
		}
	}
	
	public long getOptimizedHouseListCount(String newsId,String houseId)
			throws Exception{
		return optimizedHouseDAO.findOptimizedHouseListCount(newsId, houseId);
	}
	
	public List<OptimizedHouse> getOptimizedHouseList(String newsId,String houseId,
			int pageIndex,int pageSize) throws Exception{
		return optimizedHouseDAO.findOptimizedHouseList(newsId, houseId, pageIndex, pageSize);
	}
	
	public void deleteOptimizedHouse(String ids) throws Exception{
		if(ids == null) return ;
		for(String id :ids.split(",")){
			optimizedHouseDAO.deleteOptimizedHouse(id);
		}
	}
	
	private void deleteOptimizedHouseByOptimizedId(String optimizedId) throws Exception{
		optimizedHouseDAO.deleteOptimizedHouseByOptimizedId(optimizedId);
	}
}
